package com.qfoud.edu.aunt.commons.service.impl;

import com.qfoud.edu.aunt.commons.dao.IUserDAO;
import com.qfoud.edu.aunt.commons.dao.impl.UserDAO;
import com.qfoud.edu.aunt.commons.enum1.ResponseCode;
import com.qfoud.edu.aunt.commons.exception.BusinessException;
import com.qfoud.edu.aunt.commons.pojo.User;
import com.qfoud.edu.aunt.commons.service.IUserService;
import com.qfoud.edu.framwork.utils.StringUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * @author xiaobobo
 * @title: UserService
 * @projectName cd-java-2303-project-parent
 * @description: TODO
 * @date 2023/5/11  9:08
 */
public class UserService implements IUserService {

    private IUserDAO userDAO=new UserDAO();

    @Override
    public List<User> findUserAll() throws Exception {
        return userDAO.list();
    }

    @Override
    public void deleteById(Integer id) throws Exception {
        if(null==id){
            //说明参数参数有误
            throw new BusinessException(ResponseCode.PARAMETER_ERROR.getCode(),
                    ResponseCode.PARAMETER_ERROR.getMessage());

        }
        //程序执行到这里说明没有问题
        userDAO.deleteById(id);
    }

    @Override
    public User findUserById(Integer id) throws Exception {
        if(null==id){
            //说明参数参数有误
            throw new BusinessException(ResponseCode.PARAMETER_ERROR.getCode(),
                    ResponseCode.PARAMETER_ERROR.getMessage());

        }
        //通过id查询用户对象
       return  userDAO.findUserById(id);
    }

    @Override
    public void updateById(User user) throws Exception {
        //先进行参数判断
        if(null==user || null==user.getId()){
            //说明参数参数有误
            throw new BusinessException(ResponseCode.PARAMETER_ERROR.getCode(),
                    ResponseCode.PARAMETER_ERROR.getMessage());
        }
        //程序执行到这里 说明参数无误
        userDAO.update(user);

    }

    @Override
    public void add(User user) throws Exception {
        if(null==user || StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword())){
            //说明参数参数有误
            throw new BusinessException(ResponseCode.PARAMETER_ERROR.getCode(),
                    ResponseCode.PARAMETER_ERROR.getMessage());
        }
        //执行到这里 说明参数无误
        userDAO.insert(user);
    }
}
